package com.wyw.leetcode.learning.simple;

import com.wyw.leetcode.simple.parent.ListNode;

/**
 * leetcode topic  876
 * 链表的中间结点
 *
 * @Author Mr Wu （yewen.wu.china@gmail.com）
 * @Date 2021/11/9 10:25
 */
public class Topic876 {
    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        for(int i=2; i< 7; i++) {
            ListNode listNodeNew = new ListNode(i);
            listNodeNew.next = listNode;
            listNode = listNodeNew;
        }
        middleNode(listNode);


    }

    public static ListNode middleNode(ListNode head) {
        ListNode middle = head;
        ListNode next = head;
        while(next.next!=null) {
            if(next.next.next!= null) {
                next = next.next.next;
                middle = middle.next;
            } else {
                next = next.next;
                middle = middle.next;
            }
        }
        return middle;
    }
    public static ListNode middleNode1(ListNode head) {
        ListNode p = head, q = head;
        while (q != null && q.next != null) {
            q = q.next.next;
            p = p.next;
        }
        return p;
    }


}

